package com.baizhi.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.PmsBrand;
import com.baizhi.service.IPmsBrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhc
 * @since 2020-09-25
 */
@RestController
@RequestMapping("brand")
public class PmsBrandController {
    @Resource
    private IPmsBrandService pmsBrandService;

    @RequestMapping("selectAll")
    public Map selectAll(String name, Integer page, Integer limit){
        Page<PmsBrand> pmsBrandPage = pmsBrandService.selectAll(name, page, limit);
        Map map=new HashMap();
        map.put("code","0");
        map.put("msg","0");
        map.put("count",pmsBrandPage.getTotal());
        map.put("data",pmsBrandPage.getRecords());
        return map;
    }


    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws Exception{
        System.out.println(file.getOriginalFilename());
        String  newName= UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("E:\\idea-space\\bzmalls\\bzmall-admin\\src\\main\\webapp\\img",newName));
        Map map = new HashMap();
        map.put("newName",newName);
        return map;
    }
    @RequestMapping("insert")
    public  Map  insert(PmsBrand pmsBrand){
        Map map = new HashMap<>();
        try {
            pmsBrandService.insert(pmsBrand);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    };



    @RequestMapping("delete")
    public  Map   delete(Integer id){
        Map map = new HashMap<>();
        try {
            pmsBrandService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("selectOne")
    public  PmsBrand   selectOne(Integer id){
        return pmsBrandService.showOne(id);
    }

    @RequestMapping("update")
    public  Map  update(PmsBrand pmsBrand){
        Map map = new HashMap<>();
        try {
            pmsBrandService.update(pmsBrand);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("deleteAll")
    public  Map  deleteAll(List<PmsBrand> ids){
        Map map = new HashMap<>();
        try {
            pmsBrandService.deleteAll(ids);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse resp){
        Workbook workbook = pmsBrandService.downLoadExcel();//        文件保存本地
        try {
            // 设置 response
            resp.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("brands.xls","utf-8"));
            resp.setContentType("application/vnd.ms-excel");
            workbook.write(resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("importExcel")
    public Map importExcel (MultipartFile file) throws IOException {
        Map map = new HashMap<>();
        System.out.println(file.getOriginalFilename());
        try {
            InputStream inputStream = file.getInputStream();
            /**
             *
             *
             * 导入方法
             * 参数一  流
             * 参数二  类对象
             * 参数三  导入参数对象
             *
             */

            ImportParams importParams = new ImportParams();
            /**
             * setTitleRows(1);  声明标题占有行
             * setHeadRows(1);   声明表头占有行
             */

            importParams.setTitleRows(1);
            importParams.setHeadRows(1);

            List<PmsBrand> pmsBrands = ExcelImportUtil.importExcel(inputStream, PmsBrand.class, importParams);
            pmsBrands.forEach(pmsBrand -> System.out.println(pmsBrand));

            map.put("flag",true);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);

        }
        return map;
    }



}
